﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.Navigation;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace PublishingSiteBreadCrumb.ControlTemplates.PublishingSiteBreadCrumb
{
    public partial class SiteBreadCrumb : UserControl
    {
        string strBreadCrumbLinks = "";
        StringBuilder sbDivText = new StringBuilder();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string strBreadCrumb = "";
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    strBreadCrumb = getBreadCrumbString(SPContext.Current.File, SPContext.Current.Web);
                    var strLIs = strBreadCrumb.Split('|');
                    sbDivText.Append("<ol class=\"breadcrumb\">");

                    for (int I = 0; I < strLIs.Length; I++)
                    {
                        sbDivText.Append("<li>");
                        sbDivText.Append(strLIs[I]);
                        sbDivText.Append("</li>");

                    }
                    sbDivText.Append("</ol>");
                    divBreadCrumb.InnerHtml = sbDivText.ToString();
                });
            }
            catch (Exception ex)
            {
                string strError = ex.ToString();
            }
        }

        string getBreadCrumbString(SPFile file, SPWeb web)
        {
            string strWebTitle = "";
            string strWebLink = "";

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                strWebTitle = web.Title;
                SPFile rootfile = web.GetFile(web.RootFolder.WelcomePage);
                SPListItem item = rootfile.Item;
                strWebLink = GetPagesUrls(web);


                if (strBreadCrumbLinks.Trim().Length > 0)
                {
                    strBreadCrumbLinks = "<a href='" + strWebLink + "'>" + strWebTitle + "</a>" + "" + "&nbsp;>&nbsp;" + strBreadCrumbLinks;
                }
                else
                {
                    if (SPContext.Current.ContextPageInfo.IsWebWelcomePage && web.Url == SPContext.Current.Web.Url)
                    {
                        strBreadCrumbLinks = strWebTitle;

                    }

                    else
                    {
                        strBreadCrumbLinks = "<a href='" + strWebLink + "'>" + strWebTitle + "</a>" + "&nbsp;>&nbsp;" + file.Title;
                    }
                }

                if (!web.IsRootWeb)
                {
                    getBreadCrumbString(file, web.ParentWeb);
                }
            });
            return strBreadCrumbLinks;
        }


        public string GetPagesUrls(SPWeb web)
        {
            string retval = "";
            if (!web.IsRootWeb)
            {
                NavigationTerm term = GetWebDefaultPageTerm(web);
                if (term != null)
                {
                    retval = web.Site.RootWeb.ServerRelativeUrl + term.GetWebRelativeFriendlyUrl();

                }
                else
                {
                    retval = web.ServerRelativeUrl;
                }
            }
            else
            {
                retval = web.ServerRelativeUrl;

            }
            return retval;


        }


        public static NavigationTerm GetWebDefaultPageTerm(SPWeb web)
        {

            NavigationTerm friendlyUrlterm = null;


            if (PublishingWeb.IsPublishingWeb(web))
            {
                PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
                SPListItem DefaultPage = publishingWeb.DefaultPage.Item;

                Guid listId = PublishingWeb.GetPagesListId(web);

                SPList pagesList = web.Lists[listId];

                IList<NavigationTerm> terms = TaxonomyNavigation.GetFriendlyUrlsForListItem(DefaultPage, true);

                string url = string.Empty;


                if (terms.Count > 0)
                {
                    friendlyUrlterm = terms[0];
                }

            }


            return friendlyUrlterm;
        }
    }
}
